package org.kyw.user.service.impl;

import org.kyw.basic.constant.LoginConstant;
import org.kyw.basic.exception.BusinessException;
import org.kyw.basic.service.impl.BaseServiceImpl;
import org.kyw.basic.util.MD5Utils;
import org.kyw.basic.util.StrUtils;
import org.kyw.org.mapper.EmployeeMapper;
import org.kyw.user.domain.Logininfo;
import org.kyw.user.domain.User;
import org.kyw.user.dto.UserDto;
import org.kyw.user.mapper.LogininfoMapper;
import org.kyw.user.mapper.UserMapper;
import org.kyw.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void register(UserDto dto) {
        //空校验
        if(StringUtils.isEmpty(dto.getPhone())
                || StringUtils.isEmpty(dto.getVerifyCode())
                || StringUtils.isEmpty(dto.getPassword())
                || StringUtils.isEmpty(dto.getConfirmPassword())) {
            throw new BusinessException("请填写必要参数");
        }

        if(!dto.getPassword().equals(dto.getConfirmPassword())) {
            throw new BusinessException("两次密码不一致");
        }

        User userTmp = userMapper.loadByPhone(dto.getPhone());
        if(userTmp != null) {
            throw new BusinessException("用户已经注册，请直接登录");
        }

        Object codeTmp = redisTemplate.opsForValue().get(LoginConstant.PHONE_REGISTER + dto.getPhone());
        
        if(codeTmp == null) {
            throw new BusinessException("验证码已失效，请重新获取");
        }

        String code = codeTmp.toString().split(":")[0];
        if(!dto.getVerifyCode().equalsIgnoreCase(code)) {
            throw new BusinessException("验证码输入错误");
        }

        User user = userDto2User(dto);
        Logininfo logininfo = user2Logininfo(user);

        logininfoMapper.save(logininfo);

        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }

    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);
        return logininfo;
    }

    private User userDto2User(UserDto dto){
        User user = new User();
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        String md5Password = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Password);
        return user;
    }

    @Override
    public void update(User user) {
        super.update(user);
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
    }
}
